/**
 * MainMenu
 * 
 * 05/12/2013
 * 
 * Copyright GPL
 */
package com.jtmproject.javiertejedor.activities;

import com.jtmproject.javiertejedor.R;
import com.jtmproject.javiertejedor.asynctasks.GetWebContent;
import com.jtmproject.javiertejedor.classes.GetPreferences;
import com.jtmproject.javiertejedor.classes.StringsUtils;
import com.jtmproject.javiertejedor.cyphers.Base64Cypher;
import com.jtmproject.javiertejedor.cyphers.CaesarCypher;
import com.jtmproject.javiertejedor.cyphers.SimpleCypher;
import com.jtmproject.javiertejedor.database.DbAdapter;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This is the activity to do the work with the cipher
 * @author Javier Tejedor
 */
public class CipherActivity extends Activity implements OnClickListener {

	private EditText edtUrl;
	private EditText edtResult;
	private Button btnGet;
	private Button btnEncrypt;
	private Button btnDecrypt;
	private Button btnSaveDB;
	private Button btnCopyCP;
	private SimpleCypher simpleCypher;
	private Base64Cypher base64Cypher;
	private CaesarCypher caesarCypher;
	private DbAdapter dbAdapter;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cipher);

		chargeViews();
		setListeners();

		simpleCypher = new SimpleCypher();
		base64Cypher = new Base64Cypher();
		caesarCypher = new CaesarCypher();

		dbAdapter = new DbAdapter(this);

		edtUrl.setText("https://www.google.es/");

		printAllSecrets();
	}

	/**
	 * this prints all secrets int console
	 */
	private void printAllSecrets() {
		String id = "";
		String secret = "";
		dbAdapter.open();

		Cursor cursor = dbAdapter.getAllSecrets();

		if (cursor.moveToFirst()){
			do{
				
				id = cursor.getString(cursor.getColumnIndex("_id"));
				secret = cursor.getString(cursor.getColumnIndex("secret"));
				Log.i("Teje", "Id: " + id + "  -  " + "Secret: " + secret);
				
			}while(cursor.moveToNext());
		}
		
		cursor.close();
		dbAdapter.close();

	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_get_url:
			getUrlContent();
			break;

		case R.id.btn_encript:
			encryptText();
			break;

		case R.id.btn_decrypt:
			decryptText();
			break;

		case R.id.btn_save_db:
			saveIntoDatabase();
			break;

		case R.id.btn_copy_cp:
			saveTextCopy();
			break;
		default:
			break;
		}

	}

	/**
	 * this saves the text into the database
	 */
	private void saveIntoDatabase() {
		String result = edtResult.getText().toString();
		
		if(!StringsUtils.geStringUtils().isStringVoid(result)){
			
			dbAdapter.open();
			if(dbAdapter.insertSecret(result) > 0){
				Toast.makeText(
						this, R.string.text_saved, Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(
						this, R.string.not_saved, Toast.LENGTH_LONG).show();
			}
			dbAdapter.close();
			
			
		}else{
			Toast.makeText(this, R.string.not_text, Toast.LENGTH_LONG).show();
		}
		
	}

	/**
	 * this saves the text in the clipboard
	 */
	private void saveTextCopy(){
		String result = edtResult.getText().toString();
		if(!StringsUtils.geStringUtils().isStringVoid(result)){
			copyClipboard(result);
			Toast.makeText(
					this, R.string.text_copied, Toast.LENGTH_LONG).show();
		}else{
			Toast.makeText(this, R.string.not_text, Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * this decrypts the text
	 */
	private void decryptText() {
		String result = edtResult.getText().toString();
		if(!StringsUtils.geStringUtils().isStringVoid(result)){
			decrypt(result);
		}else{
			Toast.makeText(this, R.string.not_text, Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * this selects the cypher
	 * @param result
	 */
	private void decrypt(String text) {
		String cypherToUser =
				GetPreferences.getGetPreferences().getCipherSelected(this);

		String textCypher = "";

		if(cypherToUser.equals("simple")){
			textCypher = simpleCypher.decrypt(text);

		}else if(cypherToUser.equals("base_64")){
			textCypher = base64Cypher.decrypt(text);

		}else if(cypherToUser.equals("caesar")){
			textCypher = caesarCypher.decrypt(text);
		}

		setTextResult(textCypher);
	}

	/**
	 * this encrypts the text
	 */
	private void encryptText() {
		String result = edtResult.getText().toString();
		if(!StringsUtils.geStringUtils().isStringVoid(result)){
			encrypt(result);
		}else{
			Toast.makeText(this, R.string.not_text, Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * this selects the cypher
	 * @param text
	 */
	private void encrypt(String text){
		String cypherToUser =
				GetPreferences.getGetPreferences().getCipherSelected(this);

		String textCypher = "";

		if(cypherToUser.equals("simple")){
			textCypher = simpleCypher.encrypt(text);

		}else if(cypherToUser.equals("base_64")){
			textCypher = base64Cypher.encrypt(text);

		}else if(cypherToUser.equals("caesar")){
			textCypher = caesarCypher.encrypt(text);
		}

		setTextResult(textCypher);
	}

	/**
	 * this get the url's content
	 */
	private void getUrlContent() {
		String url = edtUrl.getText().toString();
		if(!StringsUtils.geStringUtils().isStringVoid(url)){

			new GetWebContent(this, url).execute();

		}else{
			Toast.makeText(this, R.string.not_url, Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * this copies the text passed by parameters into android's clipboard
	 * @param text
	 */
	@SuppressLint("NewApi")
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void copyClipboard(String text){
		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
		if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
			android.content.ClipboardManager clipboard =  
					(android.content.ClipboardManager) 
					getSystemService(CLIPBOARD_SERVICE); 

			ClipData clip = ClipData.newPlainText("label", text);
			clipboard.setPrimaryClip(clip); 
		} else{
			android.text.ClipboardManager clipboard = 
					(android.text.ClipboardManager)
					getSystemService(CLIPBOARD_SERVICE); 
			clipboard.setText(text);
		}
	}

	/**
	 * this sets the text into edittext "edtResul"
	 * @param text
	 */
	public void setTextResult(String text){
		edtResult.setText(text);
	}

	/**
	 * this charges the views
	 */
	private void chargeViews() {
		edtUrl  = (EditText) findViewById(R.id.edt_url);
		edtResult = (EditText) findViewById(R.id.edt_resul);
		btnGet = (Button) findViewById(R.id.btn_get_url);
		btnEncrypt = (Button) findViewById(R.id.btn_encript);
		btnDecrypt = (Button) findViewById(R.id.btn_decrypt);
		btnSaveDB = (Button) findViewById(R.id.btn_save_db);
		btnCopyCP = (Button) findViewById(R.id.btn_copy_cp);
	}

	/**
	 * this sets the listeners
	 */
	private void setListeners() {
		btnGet.setOnClickListener(this);
		btnEncrypt.setOnClickListener(this); 
		btnDecrypt.setOnClickListener(this);
		btnSaveDB.setOnClickListener(this); 
		btnCopyCP.setOnClickListener(this);
	}

}
